//package com.jhhc.StormSurgeForecast.Calculate.Controller;
//
//import cn.hutool.core.lang.UUID;
//import com.jhhc.RHDB.Data.Pojo.ST_TIDE_R;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_CALCULATESCHEME_M1;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFPATH_F;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFPATH_R;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_CALCULATESCHEME_MService;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFPATH_FService;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_TFPATH_RService;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_ST_ASTROTD_FService;
//import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.TimeUtil;
//import com.jhhc.StormSurgeForecast.Calculate.BO.CenterUrl;
//import com.jhhc.StormSurgeForecast.Calculate.BO.ModelConfig;
//import com.jhhc.StormSurgeForecast.Calculate.DTO.ModelParam;
//import com.jhhc.StormSurgeForecast.Calculate.Service.ModelService;
//import com.jhhc.StormSurgeForecast.Calculate.util.*;
//import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_OCEANMODEL_B;
//import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_RESULT_A;
//import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_OCEANMODEL_BService;
//import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_RESULT_AService;
//import com.jhhc.utils.JsonResult;
//import lombok.extern.log4j.Log4j2;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import java.io.File;
//import java.util.List;
//import java.util.Objects;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author LYQ
// */
//@RestController
//@RequestMapping(value = "StormSurgeForcast/Calculate")
//@Log4j2
//public class ModelControllerNB {
//    @Autowired
//    private BDMS_FBC_OCEANMODEL_BService bdms_fbc_oceanmodel_bService;
//
//    @Autowired
//    private BDMS_FBC_TFPATH_FService bdmsFbcTfpathFService;
//
//    @Autowired
//    private BDMS_FBC_TFPATH_RService bdmsFbcTfpathRService;
//
//    @Autowired
//    private BDMS_CALCULATESCHEME_MService bdms_calculatescheme_mService;
//
//    @Autowired
//    private BDMS_FBC_RESULT_AService bdms_fbc_result_aService;
//
//    @Autowired
//    private BDMS_ST_ASTROTD_FService bdmsStAstrotdFService;
//
//    @Autowired
//    private ModelService modelService;
//
//    @Autowired
//    private ModelConfig modelConfig;
//
//    /**
//     *模型计算
//     */
//    @RequestMapping(value = "/Forecast/Run",method = RequestMethod.POST,headers = "Accept=application/json")
//    public JsonResult run(@RequestBody ModelParam param) {
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setStatusCode(200);
//        jsonResult.setSucceeded(true);
//        String jobId = "";//作业id
//        String jobStatus = "";//作业状态
//        String code = "";//上传文件状态
//        CalUtilNB calUtil = new CalUtilNB();
//        //查询对应模型
//        BDMS_FBC_OCEANMODEL_B bdmsFbcOceanmodelB = bdms_fbc_oceanmodel_bService.
//                getModelInfo(Integer.valueOf(param.getProjectId()), param.getModelId());
//        if (bdmsFbcOceanmodelB == null || "".equals(bdmsFbcOceanmodelB.getModelid())) {
//            jsonResult.setRemark("查询模型信息失败");
//            return jsonResult;
//        }
//        //String token = calUtil.getToken(modelConfig);//获取token
//        String token = calUtil.getTokennb(modelConfig);//宁波现场
//        if (token == null || "".equals(token)) {
//            jsonResult.setRemark("获取token失败");
//            return jsonResult;
//        }
//        CenterUrl centerUrl = calUtil.getCenter(token,modelConfig);//获取授权区域地址center
//        UUID uuid1 = UUID.randomUUID();
//        String uuid = uuid1.toString();//测试加test
//        String cschemePath = modelConfig.getUserpathname() + "/model/caltmp/" + uuid;
//        //使用uidu创建文件夹,返回作业id
//        jobId = calUtil.createFolder(centerUrl.getHpcUrl(), token, cschemePath, modelConfig);
//        if (jobId != null && !"".equals(jobId)) {
//            jobStatus = calUtil.queryJobs(centerUrl.getHpcUrl(), token, jobId);
//            //查询作业状态
//            while (!"statC".equals(jobStatus)) {
//                try {
//                    TimeUnit.SECONDS.sleep(2);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                    System.out.println("线程暂停异常");
//                }
//                jobStatus = calUtil.queryJobs(centerUrl.getHpcUrl(), token, jobId);
//                System.out.println("等待作业完成中...");
//            }
//            //复制模型文件到计算方案文件夹下（uuid）
//            String modelPath = modelConfig.getUserpathname() + "model/" + bdmsFbcOceanmodelB.getFoldernm();
//            jobId = calUtil.copyFolder(centerUrl.getHpcUrl(), token, cschemePath, modelConfig, modelPath);
//            if (jobId != null && !"".equals(jobId)) {
//                //查询作业状态
//                jobStatus = calUtil.queryJobs(centerUrl.getHpcUrl(), token, jobId);
//                //查询作业状态
//                while (!"statC".equals(jobStatus)) {
//                    try {
//                        TimeUnit.SECONDS.sleep(2);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                        System.out.println("线程暂停异常");
//                    }
//                    jobStatus = calUtil.queryJobs(centerUrl.getHpcUrl(), token, jobId);
//                    System.out.println("等待作业完成中...");
//                }
//                //实测路径
//                List<BDMS_FBC_TFPATH_R> bdmsFbcTfpathRs;
//                CreateFileUtil createFileUtil = new CreateFileUtil();
//                UploadUtil uploadUtil = new UploadUtil();
//                String localPath = modelConfig.getLocalPath() + File.separator + uuid;
//                File tfpath;
//                String linuxPath = cschemePath + "/" + bdmsFbcOceanmodelB.getFoldernm();
//                TimeUtil timeUtil = new TimeUtil();
//                File astrotd;
//                File tide;
//                String workPath = cschemePath + "/" + bdmsFbcOceanmodelB.getFoldernm();
//                BDMS_CALCULATESCHEME_M1 bdmsCalculateschemeM1;
//                List<BDMS_ST_ASTROTD_F> fList;
//                List<ST_TIDE_R> rList;
//                String nhupStart;
//                String nhupEnd;
//                switch (bdmsFbcOceanmodelB.getModelid()) {
//                    case "HHUCCHAOSUAN"://东海曙光超算+构造风场
//                    case "HoHaiEastSeaZS"://东中国海模型
//                    case "HHUCCHAOSUANNS"://南海曙光超算
//                    case "HoHaiSouthSeaZs"://南中国海模型
//                    case "JSCoastSurge":
//                        //预报路径
//                        List<BDMS_FBC_TFPATH_F> bdmsFbcTfpathFs = bdmsFbcTfpathFService.forecastPath(param.getTfid(),
//                                param.getTyphoonDepart(), param.getForecastTime());
//                        if (bdmsFbcTfpathFs == null || bdmsFbcTfpathFs.size() == 0) {
//                            jsonResult.setRemark("该台风实测路径点没有对应的台风路径的预见期");
//                            return jsonResult;
//                        }
//                        bdmsFbcTfpathRs = bdmsFbcTfpathRService.getTyphoonIdPath(param.getTfid(),
//                                param.getForecastTime());
//                        if (bdmsFbcTfpathRs == null || bdmsFbcTfpathRs.size() == 0) {
//                            jsonResult.setRemark("获取台风实测路径失败");
//                            return jsonResult;
//                        }
//                        //生成tfpath.dat文件
//                        tfpath = calUtil.createTfpathFile(bdmsFbcTfpathRs, bdmsFbcTfpathFs,
//                                localPath);
//                        //上传tfpath.dat文件
//                        code = calUtil.fileUpload(centerUrl.getEfileUrl(), token, tfpath, linuxPath);
//                        if (code != null && Objects.equals(code, "0")) {
//                            System.out.println("上传成功");
//                        } else {
//                            jsonResult.setRemark("上传tfpath.dat文件失败");
//                            return jsonResult;
//                        }
//                        //生成nhup1.txt文件
//                        nhupStart = param.getForecastTime();
//                        nhupEnd = bdmsFbcTfpathFs.get(bdmsFbcTfpathFs.size() - 1).getForecasttm();
//                        File nhup = calUtil.createNhup1File(nhupStart, localPath, nhupEnd,
//                                param.getModelPar(), param.isArea(), bdmsFbcOceanmodelB);
//                        //上传nhup1.txt文件
//                        code = calUtil.fileUpload(centerUrl.getEfileUrl(), token, nhup, linuxPath);
//                        if (code != null && Objects.equals(code, "0")) {
//                            System.out.println("上传成功");
//                        } else {
//                            jsonResult.setRemark("上传nhup1.txt文件失败");
//                            return jsonResult;
//                        }
//                        //提交模型计算作业
//                        jobId = calUtil.modelCalculate(centerUrl.getHpcUrl(), token, modelConfig, workPath);
//                        bdmsCalculateschemeM1 = calUtil.insertProject(param, uuid,
//                                bdmsFbcOceanmodelB, bdmsFbcTfpathFs.get(bdmsFbcTfpathFs.size() - 1).getForecasttm(), jobId);
//                        bdms_calculatescheme_mService.insertBdmsCalculateschemeM(bdmsCalculateschemeM1);
//                        if (jobId != null && !"".equals(jobId)) {
//                            jsonResult.setData(uuid);
//                            jsonResult.setRemark("已驱动模型计算，请使用计算方案id调用获取模型计算状态接口查询计算状态");
//                            for (int i = 0; i < 1; i++) {
//                                modelService.queryByStatus(uuid, centerUrl, token, jobId, localPath,
//                                        linuxPath, param.getModelId());
//                            }
//                            return jsonResult;
//                        } else {
//                            jsonResult.setRemark("模型计算作业失败");
//                            return jsonResult;
//
//                        }
//                    case "HoHaiSmartZs": //智能预报模型
//                        nhupStart = timeUtil.dateAddHour(param.getForecastTime(),-11);
//                        nhupEnd = timeUtil.dateAddHour(param.getForecastTime(),24);
//                        bdmsFbcTfpathRs = bdmsFbcTfpathRService.searchAllByTfid(param.getTfid(),
//                                nhupStart,nhupEnd);
//                        //生成nhup1文件
//                        File localPathNhup = createFileUtil.createNuhp1(nhupStart,nhupEnd,localPath);
//                        //上传nhup1文件
//                        uploadUtil.upload(centerUrl.getEfileUrl(),token,localPathNhup,linuxPath);
//                        //生成tfpath.dat文件
//                        tfpath = createFileUtil.createTfpathFile(bdmsFbcTfpathRs,localPath);
//                        //上传tfpath.dat文件
//                        uploadUtil.upload(centerUrl.getEfileUrl(),token,tfpath,linuxPath);
//                        //生成bdms_st_astrotd_f.txt文件
//                        fList = bdmsStAstrotdFService.tide_output(
//                                timeUtil.dateAddHour(param.getForecastTime(),-23), nhupEnd);
//                        astrotd = createFileUtil.createAstrotd(fList, localPath);
//                        //上传bdms_st_astrotd_f.txt文件
//                        uploadUtil.upload(centerUrl.getEfileUrl(),token,astrotd,linuxPath);
//                        //生成st_tide_r.txt文件
//                        rList = modelService.createTide(param.getProjectId(),
//                                timeUtil.dateAddHour(param.getForecastTime(),-23), param.getForecastTime());
//                        tide = createFileUtil.createTide(rList, localPath);
//                        //上传st_tide_r.txt文件
//                        uploadUtil.upload(centerUrl.getEfileUrl(),token,tide,linuxPath);
//                        //提交模型计算作业
//                        //calUtil.modelCalculatezs(centerUrl.getHpcUrl(), token, modelConfig, workPath);
//                        jobId = calUtil.modelCalculatecw(centerUrl.getHpcUrl(), token, modelConfig, workPath);
//                        bdmsCalculateschemeM1 = calUtil.insertProject(param, uuid, bdmsFbcOceanmodelB,
//                                nhupEnd,jobId);
//                        bdms_calculatescheme_mService.insertBdmsCalculateschemeM(bdmsCalculateschemeM1);
//                        if (jobId != null && !"".equals(jobId)) {
//                            jsonResult.setData(uuid);
//                            jsonResult.setRemark("已驱动模型计算，请使用计算方案id调用获取模型计算状态接口查询计算状态");
//                            for (int i = 0; i < 1; i++) {
//                                modelService.queryByStatus(uuid, centerUrl, token, jobId, localPath,
//                                        linuxPath, param.getModelId());
//                            }
//                            return jsonResult;
//                        } else {
//                            jsonResult.setRemark("模型计算作业失败");
//                            return jsonResult;
//                        }
//                    case "HoHaiRevise": //智能校正模型
//                        nhupStart = timeUtil.dateAddHour(param.getForecastTime(),-23);
//                        nhupEnd = timeUtil.dateAddHour(param.getForecastTime(),24);
//                        //生成bdms_st_astrotd_f.txt文件
//                        fList = bdmsStAstrotdFService.tide_output(nhupStart, nhupEnd);
//                        astrotd = createFileUtil.createAstrotd(fList, localPath);
//                        //上传bdms_st_astrotd_f.txt文件
//                        uploadUtil.upload(centerUrl.getEfileUrl(),token,astrotd,linuxPath);
//                        //生成st_tide_r.txt文件
//                        rList = modelService.createTide(param.getProjectId(),nhupStart,
//                                param.getForecastTime());
//                        tide = createFileUtil.createTide(rList, localPath);
//                        //上传st_tide_r.txt文件
//                        uploadUtil.upload(centerUrl.getEfileUrl(),token,tide,linuxPath);
//                        //提交模型计算作业
//                        jobId = calUtil.modelCalculatexz(centerUrl.getHpcUrl(), token, modelConfig, workPath);
//                        bdmsCalculateschemeM1 = calUtil.insertProject(param, uuid, bdmsFbcOceanmodelB,
//                                nhupEnd,jobId);
//                        bdms_calculatescheme_mService.insertBdmsCalculateschemeM(bdmsCalculateschemeM1);
//                        if (jobId != null && !"".equals(jobId)) {
//                            jsonResult.setData(uuid);
//                            jsonResult.setRemark("已驱动模型计算，请使用计算方案id调用获取模型计算状态接口查询计算状态");
//                            for (int i = 0; i < 1; i++) {
//                                modelService.queryByStatus(uuid, centerUrl, token, jobId, localPath,
//                                        linuxPath, param.getModelId());
//                            }
//                            return jsonResult;
//                        } else {
//                            jsonResult.setRemark("模型计算作业失败");
//                            return jsonResult;
//                        }
//                    default:
//                        jsonResult.setRemark("查询模型失败");
//                        return jsonResult;
//                }
//            } else {
//                jsonResult.setRemark("复制模型文件夹作业失败");
//                return jsonResult;
//            }
//        } else {
//            jsonResult.setRemark("创建文件夹作业失败");
//            return jsonResult;
//        }
//    }
//    /**
//     *获取计算结果
//     *@param calSchemeId 获取计算方案id
//     */
//    @RequestMapping(value = "/Forecast/SaveCalResultByDomain",method = RequestMethod.GET, headers = "Accept=application/json")
//    public JsonResult saveModelCalResult(@RequestParam String calSchemeId,
//                                         @RequestParam String areaName){
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setStatusCode(200);
//        jsonResult.setSucceeded(true);
//        if (calSchemeId != null && !"".equals(calSchemeId)) {
//            //获取计算方案信息
//            BDMS_CALCULATESCHEME_M1 bdmsCalculateschemeM1 = bdms_calculatescheme_mService.selcalSchemeId(calSchemeId);
//            if (bdmsCalculateschemeM1 != null) {
//                //查询对应模型信息
//                BDMS_FBC_OCEANMODEL_B bdmsFbcOceanmodelB = bdms_fbc_oceanmodel_bService.
//                        getModelInfo(bdmsCalculateschemeM1.getPid(),bdmsCalculateschemeM1.getMdid());
//                //模型在超算上对应的数据文件夹地址
//                String linuxPath = modelConfig.getUserpathname() +"model/caltmp/" + calSchemeId + "/" +
//                        bdmsFbcOceanmodelB.getFoldernm();
//                CalUtilNB calUtil = new CalUtilNB();
//                TimeUtil timeUtil = new TimeUtil();
//                DownloadUtil downloadUtil = new DownloadUtil();
//                String token = calUtil.getTokennb(modelConfig);//宁波现场
//                if (token != null && !"".equals(token)) {
//                    CenterUrl centerUrl = calUtil.getCenter(token,modelConfig);//获取授权区域地址center
//                    boolean result;
//                    String localPath = modelConfig.getLocalPath() + File.separator +
//                            calSchemeId;
//                    if ("1".equals(bdmsCalculateschemeM1.getIsarea())) {
//                        List<BDMS_FBC_RESULT_A> as = bdms_fbc_result_aService.queryByCschemeId(calSchemeId);
//                        if (as != null && as.size() > 0) {
//                            jsonResult.setRemark("数据已存在");
//                        }else {
//                            switch (bdmsCalculateschemeM1.getMdid()){
//                                case "HHUCCHAOSUANNS"://超算模型
//                                case "HHUCCHAOSUAN":
//                                case "JSCoastSurge":
//                                    boolean exist = calUtil.fileExist(centerUrl.getEfileUrl(),
//                                            token,linuxPath + "/fort.63");
//                                    if (exist) {
//                                        result = downloadUtil.downloadFort63(centerUrl.getEfileUrl(),token,
//                                                linuxPath,localPath);
//                                        if (result) {
//                                            for (int i = 0; i < 1; i++) {
//                                                bdms_fbc_result_aService.insertCalResultByCSDomain(localPath + File.separator + "fort.63",
//                                                        timeUtil.dateAddHour(bdmsCalculateschemeM1.getDtmforecast(),bdmsFbcOceanmodelB.getOutstartindexArea()),
//                                                        calSchemeId,areaName,bdmsCalculateschemeM1.getMdid());
//                                            }
//                                            jsonResult.setRemark("预报场数据正在入库...");
//                                        }else {
//                                            bdms_calculatescheme_mService.updateArea("3",calSchemeId);
//                                            jsonResult.setRemark("下载数据失败");
//                                        }
//                                    }else {
//                                        jsonResult.setRemark("模型计算错误，未找到预报场数据文件");
//                                    }
//                                    break;
//                                case "HoHaiSouthSeaZs"://南海模型
//                                case "HoHaiEastSeaZS"://东海模型
//                                    List<String> filePaths = calUtil.fileList(centerUrl.getEfileUrl(),token,linuxPath);
//                                    if (filePaths != null && filePaths.size() > 0) {
//                                        for (int i = 0; i < 1; i++) {
//                                            bdms_fbc_result_aService.insertByDomain(filePaths,centerUrl.getEfileUrl(),
//                                                    token,localPath,calSchemeId,areaName,bdmsFbcOceanmodelB,
//                                                    bdmsCalculateschemeM1);
//                                            jsonResult.setRemark("预报场数据正在入库...");
//                                        }
//                                    }else {
//                                        jsonResult.setRemark("模型计算错误，未找到预报场数据文件");
//                                    }
//                                    break;
//                                default:
//                                    jsonResult.setRemark("预报场数据入库失败，未找到对应模型");
//                                    break;
//                            }
//                        }
//                    }else {
//                        jsonResult.setRemark("该计算方案没有输出场数据");
//                    }
//                }else {jsonResult.setRemark("获取token失败");}
//            }else {jsonResult.setRemark("该计算方案信息为空");}
//        }else {jsonResult.setRemark("参数错误");}
//        return jsonResult;
//    }
//
//    /**
//     *获取模型计算状态
//     * @param calSchemeId 计算方案id
//     */
//    @RequestMapping(value = "/Forecast/GetCalSchemeInfoByStatute",method = RequestMethod.GET, headers = "Accept=application/json")
//    public JsonResult getCalSchemeInfoByStatute(@RequestParam String calSchemeId){
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setStatusCode(200);
//        jsonResult.setSucceeded(true);
//        if (calSchemeId != null && !"".equals(calSchemeId)) {
//            BDMS_CALCULATESCHEME_M1 bdmsCalculateschemeM1 = bdms_calculatescheme_mService.
//                    selcalSchemeId(calSchemeId);
//            if (bdmsCalculateschemeM1 != null) {
//                switch (bdmsCalculateschemeM1.getState()){
//                    case "3":
//                        jsonResult.setData(3);
//                        jsonResult.setRemark("计算完成,预报站点数据已保存，可调用获取预报站点的计算结果接口进行查询；" +
//                                "预报场数据未保存,可调用保存预报场的计算结果接口进行保存");
//                        break;
//                    case "1":
//                        jsonResult.setData(1);
//                        jsonResult.setRemark("计算中");
//                        break;
//                    default:
//                        jsonResult.setData(2);
//                        jsonResult.setRemark("计算失败");
//                        break;
//                }
//            }
//        }
//        return jsonResult;
//    }
//
//}
